package cn.ytr.crm.web.controller;

import cn.ytr.crm.domain.Permission;
import cn.ytr.crm.domain.Role;
import cn.ytr.crm.query.PageResult;
import cn.ytr.crm.query.QueryObject;
import cn.ytr.crm.service.IPermissionService;
import cn.ytr.crm.service.IRoleService;
import cn.ytr.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/role")
@Controller
public class RoleController {

	@Autowired
	IRoleService roleService;
	@Autowired
    IPermissionService permissionService;

    /**
     * 显示 角色列表页面
     */
	@RequiresPermissions(value = {"访问:角色列表页面", "role:list"}, logical = Logical.OR)
	//@RequiredPermission(name = "访问角色列表页面", expression = "role:list")
	@RequestMapping("/list")
	public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
		PageResult result = roleService.query(qo);
		model.addAttribute("result", result);
		return "role/list";
	}

    /**
     * 显示 编辑或新增角色页面
     */
    @RequiresPermissions(value = {"访问:编辑或新增角色页面", "role:input"}, logical = Logical.OR)
	//@RequiredPermission(name = "访问编辑或新增角色页面", expression = "role:input")
	@RequestMapping("/input")
	public String input(Long id, Model model) {
		if (id != null) {
			Role role = roleService.get(id);
			model.addAttribute("entity", role);
		}

		//分配权限左边所有权限多选下拉框的数据共享
        List<Permission> permissions = permissionService.listAll();
        model.addAttribute("permissions", permissions);

        return "role/input";
	}

    /**
     * ajax 处理 编辑或新增角色
     */
    @ResponseBody
    @RequiresPermissions(value = {"操作:编辑或新增角色", "role:saveOrUpdate"}, logical = Logical.OR)
	//@RequiredPermission(name = "操作编辑或新增角色", expression = "role:saveOrUpdate")
	@RequestMapping("/saveOrUpdate")
	public JsonResult saveOrUpdate(Role role, Long[] permissionIds) {
    	JsonResult jsonResult = new JsonResult();
    	try {
			roleService.saveOrUpate(role, permissionIds);
    	    return jsonResult;
    	} catch (Exception e) {
    	    jsonResult.mark("服务器提示：操作失败");
    	    e.printStackTrace();
    	    return jsonResult;
    	}
	}

    /**
     * ajax 处理 删除角色
     */
    @ResponseBody
    @RequiresPermissions(value = {"操作:删除角色", "role:delete"}, logical = Logical.OR)
	//@RequiredPermission(name = "操作删除角色", expression = "role:delete")
	@RequestMapping("/delete")
	public JsonResult delete(Long id) {
    	JsonResult jsonResult = new JsonResult();
    	try {
			roleService.delete(id);
    	    return jsonResult;
    	} catch (Exception e) {
    	    jsonResult.mark("服务器提示：删除角色操作失败");
    	    e.printStackTrace();
    	    return jsonResult;
    	}
	}
	
}
